package com.itheima.stock.face.impl;

import com.itheima.stock.common.domian.UserAddDomain;
import com.itheima.stock.face.UserRolePermissionService;
import com.itheima.stock.pojo.SysPermission;
import com.itheima.stock.pojo.SysUser;
import com.itheima.stock.secuity.service.LoginUserDetailService;
import com.itheima.stock.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: siwukun
 * @date: 2022/06/16
 * @description:
 */
@Component
@CacheConfig(cacheNames = "user")
public class UserRolePermissionServiceImpl implements UserRolePermissionService {

    @Autowired
    private LoginUserDetailService loginUserDetailService;

    @Autowired
    private UserService userService;
    /**
     * 缓存登录用户对应的权限和角色
     * @param uid
     * @return
     */
    @Cacheable(key = "#root.method.getName()",unless="#result == null || #result.size() == 0")
    @Override
    public List<String> getUserRoleAndPermissionInfo(String uid) {
        return loginUserDetailService.getUserRoleAndPermissionInfo(uid);
    }



    /**
     * 用cache获取用户对应的权限
     * @param sysUser
     * @param permissions
     */
    @Cacheable(key = "#root.method.getName()",unless="#result == null")
    @Override
    public void getMenus(SysUser sysUser, List<SysPermission> permissions) {
        loginUserDetailService.getMenus(sysUser,permissions);
    }


    /**
     * 修改用户同时把用户信息缓存清除
     * @param userAddDomain
     */
    @CacheEvict(key ="'getUserRoleAndPermissionInfo'")
    @Override
    public boolean cleanUserCacheinfo(UserAddDomain userAddDomain){
        return userService.updateUserInfo(userAddDomain);
    }
}
